﻿Imports System.Web
Imports System.Web.Services
Imports System.Xml.Linq
Imports System.Data.Linq
Imports System.Linq


Public Class jsonHandler
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim resp = ""
        If context.Request.QueryString("chat") Then
            resp = "hey dear its manish here."
            Dim objChatMsg As New ChatMessage
            Select Case context.Request.QueryString("method")
                Case "postSMS"
                    Dim txt = context.Request.QueryString("m")
                    Dim f = context.Request.QueryString("f")

                    With objChatMsg
                        .Text = txt
                        .From = f
                        .Time = Now.Ticks 'Now.ToString("yyyy-MM-dd hh:mm:ss")
                        .Save()
                    End With
                Case "getAll"
                    Dim ls = objChatMsg.BuildChatMessageListHtml
                Case "getFrom"

            End Select

            


        Else
            Dim s As New List(Of String)
            For i = 0 To 5
                Dim o As New Random
                s.Add(o.Next(12550, 199960))
            Next
            resp = String.Join("\n", s.ToArray)
        End If

        context.Response.Write(resp)
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property


End Class

Public Class ChatMessage
    Property Text As String
    Property Time As Long
    Property From As String

    ReadOnly Property DisplayTime As String
        Get
            Dim dt = Date.FromBinary(Me.Time)
            If dt.Day = Now.Day Then
                DisplayTime = dt.ToString("h:mm tt")
            ElseIf dt.Day = Now.Day - 1 Then
                DisplayTime = "Yesterday"
            Else
                DisplayTime = dt.ToString("dd-MMM")
            End If



        End Get
    End Property

    Function BuildChatMessageListHtml() As String
        Dim o = ListAll.Result

        Dim x = <html>
                    <head>
                        <link href="Css/sms.css" rel="stylesheet" type="text/css"/>
                    </head>
                    <body>
                        <form method="post" action=<%= HttpContext.Current.Request.Url.ToString %>>
                            <textarea id="txtMsg" name="s" rows="4"></textarea>
                            <input type="hidden" name="f" value=<%= HttpContext.Current.Request.QueryString("f") %>></input>
                            <a href=<%= HttpContext.Current.Request.Url.ToString %>>Refresh</a>
                            <input id="btnSend" type="submit" value="Send"/>
                        </form>
                        <ul>
                            <%= From t In o Order By t.Time Descending Take 10 _
                                Select <li>
                                           <b><%= t.From & " : " %></b>
                                           <span><%= t.Text %></span>
                                           <i><%= t.DisplayTime %></i>
                                       </li> %>
                        </ul>
                    </body>
                </html>
        Return x.ToString(System.Xml.Linq.SaveOptions.DisableFormatting)
    End Function

    Function ListAll() As BOResult(Of List(Of ChatMessage))
        Dim obj As New BOResult(Of List(Of ChatMessage))
        Dim lstChat As New List(Of ChatMessage)

        Dim fs = String.Join("", (From t In My.Computer.FileSystem.GetFiles(ChatDataFolder) Select My.Computer.FileSystem.ReadAllText(t)).ToArray)
        Dim data = String.Format("<x>{0}</x>", fs)
        Dim x = System.Xml.Linq.XElement.Parse(data)

        Dim ckck = From t As XElement In x...<sms> Select t...<text>.Value()

        Dim ls = From t As XElement In x...<sms> _
                 Select New ChatMessage With {.Text = t...<text>.Value, .From = t...<from>.Value, .Time = t...<time>.Value}

        obj.Result = ls.ToList

        Return obj

    End Function

    Sub Save()
        If Not String.IsNullOrEmpty(Me.From) Then
            Dim x = <sms><text><%= Me.Text %></text><time><%= Me.Time %></time><from><%= Me.From %></from></sms>
            Dim f = HttpContext.Current.Server.MapPath("~/Chat/Data/" & Me.From & ".txt")
            My.Computer.FileSystem.WriteAllText(f, x.ToString, True)
        End If

    End Sub

    Function loadFrom(ByVal f As String) As XElement
        Dim x = XElement.Load(ChatDataPath(f))
        Return x
    End Function

    ReadOnly Property ChatDataFolder() As String
        Get
            Return HttpContext.Current.Server.MapPath("~/Chat/Data/")
        End Get
    End Property

    ReadOnly Property ChatDataPath(ByVal f As String) As String
        Get
            Return HttpContext.Current.Server.MapPath("~/Chat/Data/" & f & ".xml")
        End Get
    End Property


End Class

